package com.lbs.jiepiaisi.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.maps.Projection;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.LatLngBounds;
import com.amap.api.maps.model.MarkerOptions;
import com.lbs.jiepiaisi.R;

import java.util.ArrayList;

public class MyGaodeImageView {
    // 上下文
    private Activity activity;
    // marker类
    private MarkerOptions options;
    // 当前可观区域里的 聚合过之后的集合
    private ArrayList<MarkerOptions> includeMarkers;
    // 创建区域
    private LatLngBounds bounds;

    /**
     * 头像加载完监听
     */
    public MyGaodeImageView(Activity activity, MarkerOptions firstMarkers, Projection projection, int gridSize) {
        this.activity = activity;
        options = new MarkerOptions();

        Point point = projection.toScreenLocation(firstMarkers.getPosition());
        // 范围类
        Point southwestPoint = new Point(point.x - gridSize, point.y + gridSize);
        // 范围类
        Point northeastPoint = new Point(point.x + gridSize, point.y - gridSize);

        bounds = new LatLngBounds(projection.fromScreenLocation(southwestPoint),
                projection.fromScreenLocation(northeastPoint));
        // 设置初始化marker属性
        options.anchor(0.5f, 0.5f).title(firstMarkers.getTitle()).position(firstMarkers.getPosition());
        // .icon(firstMarkers.getIcon()).snippet(firstMarkers.getSnippet());
        includeMarkers = new ArrayList<MarkerOptions>();
        includeMarkers.add(firstMarkers);
    }

    public LatLngBounds getBounds() {
        return bounds;
    }

    public MarkerOptions getOptions() {
        return options;
    }

    public void setOptions(MarkerOptions options) {
        this.options = options;
    }

    /**
     * 添加marker
     */
    public void addMarker(MarkerOptions markerOptions) {
        includeMarkers.add(markerOptions);// 添加到列表中
    }

    /**
     * 设置聚合点的中心位置以及图标
     */
    public void setpositionAndIcon() {
        int size = includeMarkers.size();
        double lat = 0.0;
        double lng = 0.0;
        // 一个的时候
        if (size == 1) {// 设置marker单个属性
            // 设置marker位置
            options.position(new LatLng(includeMarkers.get(0).getPosition().latitude,
                    includeMarkers.get(0).getPosition().longitude));
            //options.title("聚合点");
            // options.icon(BitmapDescriptorFactory.fromBitmap(getViewBitmap(getView(size))));

        } else {// 聚合的时候
            // 设置marker聚合属性
            for (MarkerOptions op : includeMarkers) {
                lat += op.getPosition().latitude;
                lng += op.getPosition().longitude;
            }
            // 设置marker的位置为中心位置为聚集点的平均位置
            options.position(new LatLng(lat / size, lng / size));
            options.icon(BitmapDescriptorFactory.fromBitmap(getViewBitmap(getView(size))));
            // int iconType = size / 10;
            //
            // switch (iconType) {
            // case 10:
            // options.icon(
            // BitmapDescriptorFactory.fromBitmap(getViewBitmap(getView(size,
            // R.drawable.marker_cluster_10))));
            // break;
            // case 50:
            // options.icon(
            // BitmapDescriptorFactory.fromBitmap(getViewBitmap(getView(size,
            // R.drawable.marker_cluster_20))));
            // break;
            // // case 2:
            // // options.icon(BitmapDescriptorFactory
            // // .fromBitmap(getViewBitmap(getView(size,
            // // R.drawable.marker_cluster_30))));
            // // break;
            // // case 3:
            // // options.icon(BitmapDescriptorFactory
            // // .fromBitmap(getViewBitmap(getView(size,
            // // R.drawable.marker_cluster_30))));
            // // break;
            // // case 4:
            // // options.icon(BitmapDescriptorFactory
            // // .fromBitmap(getViewBitmap(getView(size,
            // // R.drawable.marker_cluster_50))));
            // // break;
            // default:
            // options.icon(BitmapDescriptorFactory
            // .fromBitmap(getViewBitmap(getView(size,
            // R.drawable.marker_cluster_100))));
            // break;
            // }
        }
    }

    public View getView(int carNum) {
        View view = activity.getLayoutInflater().inflate(R.layout.my_car_cluster_view, null);
        TextView carNumTextView = (TextView) view.findViewById(R.id.my_car_num);
        RelativeLayout myCarLayout = (RelativeLayout) view.findViewById(R.id.my_car_bg);
        myCarLayout.setBackgroundResource(R.mipmap.icon_daohang);
        carNumTextView.setText(String.valueOf(carNum));
        return view;
    }

    /**
     * 把一个view转化成bitmap对象
     */
    public static Bitmap getViewBitmap(View view) {
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }
}
